/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		Console.cpp

					E-mail:		arise_engine@mail.ru

created:			13.09.2008
last changes:		19.09.2008
*****************************************************************************************/
#include "stdafx.h"


Console::Console()
{
	m_scene					= 0;
	m_background			= 0;
	m_font					= 0;
	m_num_strings_visible	= 20;
	m_offset_width			= 10;
	m_offset_height			= 10;
	m_current_string		= 0;
}
Console::~Console()
{
}


bool Console::Init(IScene2d* scene)
{
	if(!scene) return false;

	m_scene = scene;
	FileSystem* file_system = m_scene->m_file_system;

	ConfigTable table;
	FileData* tfile = file_system->GetFile("Data\\console.cfg");
	if(!tfile)return false;
	if(!table.LoadFromFile(tfile))return false;
	SAFE_DELETE(tfile);	//	free memory

	//	common settings
	m_offset_width			= table.GetFloat("console", "offset_width", m_offset_width);
	m_offset_height			= table.GetFloat("console", "offset_height", m_offset_height);
	m_num_strings_visible	= table.GetInt("console", "num_strings", m_num_strings_visible);
	float offset_edit		= table.GetFloat("console", "offset_edit", 12);

	//	graphic settings
	m_background	= m_scene->CreateSprite("background", &table);
	m_font			= m_scene->CreateFont("font", &table);

	m_colors[0]	= m_scene->CreateColor("colors", "default", &table);
	m_colors[1]	= m_scene->CreateColor("colors", "red", &table);
	m_colors[2]	= m_scene->CreateColor("colors", "orange", &table);
	m_colors[3]	= m_scene->CreateColor("colors", "yellow", &table);
	m_colors[4]	= m_scene->CreateColor("colors", "green", &table);
	m_colors[5]	= m_scene->CreateColor("colors", "gray", &table);
	m_colors[6]	= m_scene->CreateColor("colors", "darkviolet", &table);

	m_font->SetColor(m_colors[0]);	//	set default color

	if(!m_background || !m_font) return false;

	//	scale height: num strings + one extra for input + offsets + edit offset
	float height = m_offset_height * 2 + (m_num_strings_visible + 1) * m_font->GetHeight() + offset_edit;
	
	//	scale width on screen dimention
	if(scene->m_settings) m_background->ResizeDimensions((float)scene->m_settings->window_width, height);

	float edit_pos_y	= m_offset_height + m_num_strings_visible * m_font->GetHeight() + offset_edit;
	float edit_width	= (float)scene->m_settings->window_width - (m_offset_width * 2);

	m_edit.Init(0, m_offset_width, edit_pos_y, edit_width, m_font);
	m_edit.active = true;

	return true;
}


void Console::Update(float delta_time)
{
	if(!m_scene) return;

	m_edit.Update(delta_time);
}


void Console::Render()
{
	if(!m_scene) return;

	if(m_background) m_background->Render();

	ShowStrings(m_current_string);

	m_edit.Render();
}


void Console::ShowStrings(UINT start_string)
{
	if(!m_font) return;
	if(start_string < 0 || start_string >= m_strings.size()) return;

	for(UINT i = 0; i < m_num_strings_visible; i++)
	{
		if(i >= m_strings.size()) return;

		m_font->SetColor(m_colors[m_strings[start_string + i].color]);

		m_font->printf(	m_offset_width, 
						m_offset_height + m_font->GetHeight() * i, 
						ALIGN_TEXT_LEFT, 
						m_strings[start_string + i].string.c_str()		);
	}

}


void Console::Release()
{
	SAFE_RELEASE(m_background);
	SAFE_DELETE(m_font);
	m_strings.clear();
}


void Console::AddString(const std::string& string, UINT color)
{
	ConsoleString console_string;
	console_string.color = color;
	console_string.string = string;
	m_strings.push_back(console_string);
}


void Console::KeyClick(int key, int character)
{
	m_edit.KeyClick(key, character);
}



